package com.hejia.alauda.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.hejia.alauda.Constants;
import com.hejia.alauda.entity.PushMessage;
import com.hejia.alauda.entity.example.PushMessageExample;
import com.hejia.alauda.persistence.mybatis.PushMessageMapper;
import com.umeng.config.UmengConfig;
import com.umeng.push.UmengPush;

@Service
public class PushMessageServiceImpl implements PushMessageService {

	@Resource
	private Environment environment;

	@Resource
	private PushMessageMapper pushMessageMapper;

	@Override
	public boolean pushMessage(UmengConfig config, PushMessage pushMessage) throws Exception {

		if (StringUtils.isNotEmpty(pushMessage.getOpenUrl())) {
			if (!pushMessage.getOpenUrl().startsWith("http")) {
				throw new RuntimeException("URL地址格式错误");
			}
		}

		UmengPush umengPush = new UmengPush(config);
		if (pushMessage.getModels().equals(PushMessage.PUSH_MESSAGE_MODEL_IOS)) {
			if (pushMessage.getUser() == null) {
				// 向所有用户推送
				return umengPush.sendIOSBroadcast(pushMessage);
			} else {
				// 向指定用户推送
				return umengPush.sendIOSCustomizedcast(pushMessage);
			}
		} else if (pushMessage.getModels().equals(
				PushMessage.PUSH_MESSAGE_MODEL_ANDROID)) {
			if (pushMessage.getUser() == null) {
				// 向所有用户推送
				return umengPush.sendAndroidBroadcast(pushMessage);
			} else {
				// 向指定用户推送
				return umengPush.sendAndroidCustomizedcast(pushMessage);
			}
		}

		return false;
	}

	@Override
	public boolean pushIOSMessage(UmengConfig config, PushMessage pushMessage) throws Exception {
		pushMessage.setModels(PushMessage.PUSH_MESSAGE_MODEL_IOS);
		return this.pushMessage(config, pushMessage);
	}

	@Override
	public boolean pushAndroidMessage(UmengConfig config, PushMessage pushMessage) throws Exception {
		pushMessage.setModels(PushMessage.PUSH_MESSAGE_MODEL_ANDROID);
		return this.pushMessage(config, pushMessage);
	}

	@Override
	public List<PushMessage> findTimingPushMessages() {
		DateTime now = new DateTime();
		PushMessageExample example = new PushMessageExample();
		example.appendCriterion("pushTime >", now.minusDays(1).toDate());
		example.appendCriterion("pushTime <", now.toDate());
		example.appendCriterion("pushTime is not null");
		example.appendCriterion("submitTime is null");
		return this.pushMessageMapper.selectByExample(example, null);
	}

	@Override
	public void pushTimingMessage(UmengConfig config, Long id) throws Exception {

		PushMessage pushMessage = this.pushMessageMapper.selectByPrimaryKey(id);
		DateTime now = new DateTime();

		// 检查
		if (pushMessage.getPushTime() == null) {
			return;
		}
		if (pushMessage.getPushTime().after(now.toDate())) {
			return;
		}
		if (pushMessage.getSubmitTime() != null) {
			return;
		}

		if (!environment.acceptsProfiles(Constants.PROFILE_PRODUCTION)) {
			pushMessage.setProductionMode(PushMessage.PRODUCTION_MODE_FALSE);
		}
		this.pushMessage(config, pushMessage);

		pushMessage.setSubmitTime(now.toDate());
		this.pushMessageMapper.updateByPrimaryKey(pushMessage);
	}

	@Override
	public boolean deletePushMessage(Long id) {
		return pushMessageMapper.deleteByPrimaryKey(pushMessageMapper
				.selectByPrimaryKey(id)) > 0;
	}

	@Override
	public PushMessage getPushMessageById(Long id) {
		return pushMessageMapper.selectByPrimaryKey(id);
	}
}
